package com.atom.app.mvi.ui.main.f1

import com.atom.app.mvi.ui.main.UiEvent
import com.atom.module.mvi.core.element.Reducer
import com.atom.module.mvi.core.feature.ReducerFeature


class Feature1 : ReducerFeature<UiEvent, Feature1.State, Nothing>(
    initialState = State(), reducer = ReducerImpl()
) {
    data class State(
        val counter: Int = 0, val activeButtonIdx: Int? = null
    )

    class ReducerImpl : Reducer<State, UiEvent> {
        override fun invoke(state: State, wish: UiEvent): State = when (wish) {
            UiEvent.PlusClicked -> state.copy(
                counter = state.counter + 1
            )
            is UiEvent.ButtonClicked -> state.copy(
                activeButtonIdx = if (wish.idx != state.activeButtonIdx) wish.idx else null
            )
            else -> {
                state
            }
        }
    }
}
